/*
 * Package: org.grassfield.common.view
 * FileName: UserRoleAction.java
 *  
 * Created by pandian on Jan 28, 2011
 */
package action;

import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import service.IMenuListService;
import service.IUserRoleService;

import bean.MenuItem;
import bean.UserRole;

import com.opensymphony.xwork2.ActionSupport;


/**
 * The Class UserRoleView.
 */
public class UserRoleAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(UserRoleAction.class);
	
	private IUserRoleService userRoleService;
	private IMenuListService menuListService;
	private List<UserRole> userRoleList;
	private UserRole newUserRole;
	private List<MenuItem> menuList;
	private Long[] selectedMenuItem;
	
	/**
	 * Sets the user role service.
	 * 
	 * @param userRoleService the new user role service
	 */
	public void setUserRoleService(IUserRoleService userRoleService) {
		this.userRoleService = userRoleService;
	}


	/**
	 * Gets the user role list.
	 * 
	 * @return the user role list
	 */
	public List<UserRole> getUserRoleList() {
		return userRoleList;
	}


	/**
	 * Sets the user role list.
	 * 
	 * @param userRoleList the new user role list
	 */
	public void setUserRoleList(List<UserRole> userRoleList) {
		this.userRoleList = userRoleList;
	}


	/**
	 * Gets the new user role.
	 * 
	 * @return the new user role
	 */
	public UserRole getNewUserRole() {
		return newUserRole;
	}


	/**
	 * Sets the new user role.
	 * 
	 * @param newUserRole the new new user role
	 */
	public void setNewUserRole(UserRole newUserRole) {
		this.newUserRole = newUserRole;
	}


	/**
	 * Gets the menu list.
	 * 
	 * @return the menu list
	 */
	public List<MenuItem> getMenuList() {
		return menuList;
	}


	/**
	 * Gets the selected menu item.
	 * 
	 * @return the selected menu item
	 */
	public Long[] getSelectedMenuItem() {
		return selectedMenuItem;
	}


	/**
	 * Sets the selected menu item.
	 * 
	 * @param selectedMenuItem the new selected menu item
	 */
	public void setSelectedMenuItem(Long[] selectedMenuItem) {
		this.selectedMenuItem = selectedMenuItem;
	}

	/**
	 * Sets the menu list service.
	 * 
	 * @param menuListService the new menu list service
	 */
	public void setMenuListService(IMenuListService menuListService) {
		this.menuListService = menuListService;
	}

	/**
	 * User role list.
	 * 
	 * @return the string
	 */
	public String list(){
		this.userRoleList = this.userRoleService.list();
		return "list";
	}
	
	/**
	 * Adds the.
	 * 
	 * @return the string
	 */
	public String add(){
		this.menuList = this.menuListService.getMenuList();
		try {
			if (newUserRole!=null){
				System.out.println("Role name:"+this.newUserRole.getName());
				this.newUserRole.setMenuList(this.menuListService.getMenuList(this.selectedMenuItem));
				if (this.newUserRole.getId()==null){
					//add new
					newUserRole=this.userRoleService.add(newUserRole);
					return this.list();
				}else{
					this.userRoleService.update(newUserRole);
				}
			}
		} catch (Exception e) {
			this.logger.error("Error while adding user", e);
			super.addActionError(e.toString());
		}
		return "add";
	}
	
	/**
	 * Modify.
	 * 
	 * @return the string
	 */
	public String modify(){
		logger.debug("user role modify");
		if (newUserRole!=null && newUserRole.getName()==null){
			logger.debug("user role modify 1");
			this.menuList = this.menuListService.getMenuList();
			System.out.println("Status:entering....");
			this.newUserRole = this.userRoleService.find(this.newUserRole.getId());
			this.selectedMenuItem = this.menuListService.getArray(newUserRole);
			System.out.println("newUserRole1:"+newUserRole);
		}else if (newUserRole!=null && newUserRole.getName()!=null){
			logger.debug("user role modify 2");
			System.out.println("Status:processing....");
			UserRole userRole = this.userRoleService.find(this.newUserRole.getId());
			userRole.setName(this.newUserRole.getName());
			logger.debug("email permission1:"+this.newUserRole.getEmailPermission());
			userRole.setEmailPermission(this.newUserRole.getEmailPermission());
			logger.debug("email permission2:"+userRole.getEmailPermission());
			userRole.setMenuList(null);
			this.userRoleService.update(userRole);
			logger.debug("email permission3:"+userRole.getEmailPermission());
			
			Set<MenuItem> selectedMenuList = this.menuListService.getMenuList(this.selectedMenuItem);
			System.out.println("selectedMenuList:"+selectedMenuList);
			userRole.setMenuList(selectedMenuList);
			try {
				this.userRoleService.update(userRole);
				logger.debug("email permission4:"+userRole.getEmailPermission());
				return this.list();
			} catch (Exception e) {
				e.printStackTrace();
				super.addActionError(e.toString());
				this.menuList = this.menuListService.getMenuList();
			}
			
		}else{
			logger.debug("user role modify 3");
			System.out.println("Status:adding....");
			return this.add();
		}
		return "modify";
	}
}
